/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Asignafichas;
import co.edu.senaclem.modelo.Fichatitulacion;
import co.edu.senaclem.modelo.Instructor;
import co.edu.senaclem.modelo.Programa;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author anfeles
 */
@Stateless
public class FichaTitulacionDAO implements IFichaTitulacionDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void eliminar(Fichatitulacion fichaTitulacion) throws Exception {
        try {
            entityManager.remove(fichaTitulacion);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Fichatitulacion fichaTitulacion) throws Exception {
        try {
            entityManager.merge(fichaTitulacion);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void insertar(Fichatitulacion fichaTitulacion) throws Exception {
        try {
            entityManager.persist(fichaTitulacion);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Fichatitulacion consultarPorId(int ficha) throws Exception {
        try {
            return entityManager.find(Fichatitulacion.class, ficha);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Fichatitulacion> consultar() throws Exception {
        try {
            String jpql = "Select f From Fichatitulacion f";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Fichatitulacion> consultarDisponibles(Instructor instructor) throws Exception {
        try {
            String jpql = "select * from Fichatitulacion model where model.fichatitulacion NOT IN(select model.fichatitulacion from Fichatitulacion model, Asignafichas a, Instructor i where a.fichatitulacion = model.fichatitulacion and a.documentoinstructor=i.documentoinstructor and i.documentoinstructor='" + instructor.getDocumentoinstructor() + "')";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Fichatitulacion> consultarAsignadas(Instructor instructor) throws Exception {
        try {
            String consulta = "select * from Fichatitulacion model, Asignafichas a where a.fichatitulacion = model.fichatitulacion and a.documentoinstructor='" + instructor.getDocumentoinstructor() + "'";
            Query query = entityManager.createQuery(consulta);
            return query.getResultList();
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void registrarFichas(Asignafichas asignafichas) throws Exception {
        try {
           entityManager.persist(asignafichas);
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void retirarFichas(Asignafichas asignafichas) throws Exception {
        try {
           entityManager.remove(asignafichas);
        } catch (RuntimeException e) {
            throw e;
        }
    }

}
